//
//  SettingViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/17.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingViewCell.h"
#import "LogoutViewCell.h"
#import "LogoutAlertView.h"
#import "EMClient.h"
#import "DMLoginViewController.h"
#import "AppDelegate.h"

#define keyWindow [UIApplication sharedApplication].keyWindow

@interface SettingViewController ()<UITableViewDataSource, UITableViewDelegate>{

    NSArray *_titles;
}

@property (nonatomic, strong)UITableView *tableView;

//蒙版
@property (nonatomic, strong)UIView *vCover;

//退出/取消View
@property (nonatomic, strong)LogoutAlertView *logoutAlertView;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initViews];
    [self layoutViews];
}

- (void)initViews {
    
    _titles = @[@[@"账号与安全"], @[@"新消息通知", @"隐私", @"通用"], @[@"帮助与反馈", @"关于微信"], @[@"退出登录"]];

    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = KBACKGROUND_COLOR;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width_sd, 20)];
    
    [self.view addSubview:self.tableView];
    
    
}


- (void)layoutViews {

    self.tableView.sd_layout
    .topSpaceToView(self.view ,KSTATUS_AND_NAVIGATION_HEIGHT)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view);
    
    
    
}

#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *titlesOfSection = _titles[section];
    
    return titlesOfSection.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reusedId = @"setting";
    static NSString *reusedId_logout = @"logout";
    
    NSArray *titlesOfSection = _titles[indexPath.section];
    
    if (indexPath.section != 3) {
        
        SettingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId];
        
        if (!cell) {
            
            cell = [[SettingViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
        }
        
        cell.labTitle.text = titlesOfSection[indexPath.row];
        
        //显示隐藏保护
        if (indexPath.section == 0) {
            
            [cell showLock];
        } else {
        
            [cell hideLock];
        }
        
        return cell;
    }
    else {
    
        LogoutViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId_logout];
        
        if (!cell) {
            
            cell = [[LogoutViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId_logout];
        }
        
        cell.labLogout.text = titlesOfSection[indexPath.row];
        
        return cell;
    }
    
    return 0;
}

#pragma mark - tableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 14.0;
    }
    
    return 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    static NSString *headerReusedId = @"header";
    
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReusedId];
    
    if (!headerView) {
        
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerReusedId];
        
    }
    
    headerView.contentView.backgroundColor = KBACKGROUND_COLOR;
    
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    
    
    //退出登录
    if (indexPath.section == _titles.count-1) {
        
        [self didClickedlogout];
    }
}

//取消tableView-headerView的粘性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 20;
        
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
}

#pragma mark 退出
- (void)didClickedlogout {

    DMLog(@"退出登录");
    
    //蒙版
    self.vCover = [[UIView alloc] init];
    self.vCover.backgroundColor = HEXColor(@"7f7f7f");
    self.vCover.alpha = 0;
    
    self.vCover.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideCoverAndAlertView)];
    [self.vCover addGestureRecognizer:tap];
    
    //退出/取消退出View
    self.logoutAlertView = [[LogoutAlertView alloc] init];
    
    __block typeof(self) weakSelf = self;
    self.logoutAlertView.logoutBlock = ^(){
        
        //退出登录
        DMLog(@"退出");
        
        [MBProgressHUD showMessage:nil toView:weakSelf.view];
        dispatch_async(KQUEUE_GLOBAL, ^{
            
            [[EMClient sharedClient] logout:YES completion:^(EMError *aError) {
                
                if (!aError) {
                    
                    dispatch_async(KQUEUE_MAIN, ^{
                        
                        DMLog(@"退出成功!");
                        
                        DMLoginViewController *loginVC = [[DMLoginViewController alloc] init];
                        
                        [weakSelf.navigationController presentViewController:loginVC animated:YES completion:nil];
                        
                    });
                    
                } else {
                    
                    [MBProgressHUD showError:aError.debugDescription toView:weakSelf.view];
                }
            }];
            
            
            
            
        });
        
        [MBProgressHUD hideHUDForView:weakSelf.view];

    };
    
    self.logoutAlertView.cancelBlock = ^(){
        
        //取消退出
        DMLog(@"取消");
        [weakSelf hideCoverAndAlertView];
    };
    
    [self.view addSubview:self.vCover];
    [self.view addSubview:self.logoutAlertView];
    
    self.vCover.sd_layout
    .leftEqualToView(keyWindow)
    .rightEqualToView(keyWindow)
    .topEqualToView(keyWindow)
    .bottomEqualToView(keyWindow);
    
    self.logoutAlertView.frame = CGRectMake(0, self.view.height_sd, self.view.width_sd, 178);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.vCover.alpha = 0.5;
        self.logoutAlertView.frame = CGRectMake(0, self.view.height_sd-178, self.view.width_sd, 178);
    }];

}

//隐藏蒙版和退出确认View
- (void)hideCoverAndAlertView {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.vCover.alpha = 0;
        self.logoutAlertView.frame = CGRectMake(0, self.view.height_sd, self.view.width_sd, 178);
        
    } completion:^(BOOL finished) {
        
        [self.vCover removeFromSuperview];
        [self.logoutAlertView removeFromSuperview];
    }];
}

- (void)dealloc {

    DMLog(@"setting dealloc");
}


@end
